﻿#include<iostream>  
#include<windows.h>  
#include<conio.h>  
#include<fstream>
#include<vector>
#include<time.h>  
#include<string>  
#include<stdlib.h>
#include<iomanip>
#include<thread>
#include"Game.h"
#include"CharacterVideo.h"

using namespace std;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
#define BLUE       0x0001  
#define INTENSITY  0x0008  
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);

/*=============== all the structures ===============*/



/*=============== all the functions ===============*/

void SetPos(COORD a)// set cursor   
{
	HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(out, a);//使光标 到a（x，y）这个位置的函数.
}

void SetPos(int i, int j)// set cursor  
{
	COORD pos = { i, j };
	SetPos(pos);
}//输入坐标

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}//隐藏光标
void SetDrawColor(int iColor, BOOL bForeground)
{
	if (bForeground)
		SetConsoleTextAttribute(out, iColor);
	else
		SetConsoleTextAttribute(out, iColor << 4);
}
void skillbegin()
{
	SetPos(10, 29);
	cout << "**************************";

	SetPos(10, 30);
	cout << "技     能     释     放";
	SetPos(10, 31);
	cout << "**************************";
	Sleep(600);


}
void DrawRectangle(int ix1, int iy1, int iColor)
{
	thread t1(skillbegin);//设置线程
	t1.detach();//线程与主程序同步运行
	SetDrawColor(iColor, FALSE);
	SetPos(ix1, iy1 - 1);
	cout << "▍";
	Sleep(100);
	SetPos(ix1, iy1 - 1);
	cout << "▋";
	Sleep(100);
	SetPos(ix1, iy1 - 1);
	cout << "▊";
	Sleep(100);
	SetPos(ix1, iy1 - 1);
	cout << "▉";
	Sleep(100);


	for (int i = 2; i < iy1; i++)
	{
		SetPos(ix1, i);
		cout << "  ";
	}
	Sleep(100);
	SetConsoleTextAttribute(out, FOREGROUND_INTENSITY);//恢复默认的灰色
	for (int i = 1; i < iy1; i++)
	{
		SetPos(ix1, i);
		cout << "  ";
	}
	SetPos(10, 29);
	cout << "                                  ";
	SetPos(10, 30);
	cout << "                                   ";
	SetPos(10, 31);
	cout << "                                  ";

}
void blue_border()
{
	WORD blue = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN;//设置字体颜色、背景颜色
	SetConsoleTextAttribute(hConsole, blue);//字体样式
}
void white_back()
{
	WORD white = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
	SetConsoleTextAttribute(hConsole, white);
}
void cyan_choose()
{
	WORD cyan = FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE;
	SetConsoleTextAttribute(hConsole, cyan);
}
void on_Select()
{
	WORD select = FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_RED;
	SetConsoleTextAttribute(hConsole, select);
}
void onChoose(int x, int y)
{
	if (x == 25 && y == 10)
	{
		on_Select();
		cout << "1. 开始 ";
	}
	else if (x == 25 && y == 20)
	{
		on_Select();
		cout << "3. 积分榜 ";
	}
	else if (x == 65 && y == 10)
	{
		on_Select();
		cout << "2. 继续 ";
	}
	else if (x == 65 && y == 20)
	{
		on_Select();
		cout << "4. 退出 ";
	}
}
void star()
{
	cyan_choose();
	SetPos(25, 10);
	cout << "1. 开始 ";
	SetPos(65, 10);
	cout << "2. 继续 ";
	SetPos(25, 20);
	cout << "3. 积分榜 ";
	SetPos(65, 20);
	cout << "4. 退出 ";
}


//把第y行，[x1, x2) 之间的坐标填充为 ch  
void drawRow(int y, int x1, int x2, char ch)
{
	SetPos(x1, y);
	for (int i = 0; i <= (x2 - x1); i++)
		cout << ch;
}

//在a, b 纵坐标相同的前提下，把坐标 [a, b] 之间填充为 ch  
void drawRow(COORD a, COORD b, char ch)
{
	if (a.Y == b.Y)
		drawRow(a.Y, a.X, b.X, ch);
	else
	{
		SetPos(0, 25);
		cout << "error code 01：无法填充行，因为两个坐标的纵坐标(x)不相等";
		system("pause");
	}
}

//把第x列，[y1, y2] 之间的坐标填充为 ch  
void drawCol(int x, int y1, int y2, char ch)
{
	int y = y1;
	while (y != y2 + 1)
	{
		SetPos(x, y);
		cout << ch;
		y++;
	}
}

//在a, b 横坐标相同的前提下，把坐标 [a, b] 之间填充为 ch  
void drawCol(COORD a, COORD b, char ch)
{
	if (a.X == b.X)
		drawCol(a.X, a.Y, b.Y, ch);
	else
	{
		SetPos(0, 25);
		cout << "error code 02：无法填充列，因为两个坐标的横坐标(y)不相等";
		system("pause");
	}
}

//左上角坐标、右下角坐标、用row填充行、用col填充列  
void drawFrame(COORD a, COORD  b, char row, char col)
{
	drawRow(a.Y, a.X + 1, b.X - 1, row);
	drawRow(b.Y, a.X + 1, b.X - 1, row);
	drawCol(a.X, a.Y + 1, b.Y - 1, col);
	drawCol(b.X, a.Y + 1, b.Y - 1, col);
}

void drawFrame(int x1, int y1, int x2, int y2, char row, char col)
{
	COORD a = { x1, y1 };
	COORD b = { x2, y2 };
	drawFrame(a, b, row, col);
}

void drawFrame(Frame frame, char row, char col)//跟xxtoNULL函数有关
{
	COORD a = frame.position[0];
	COORD b = frame.position[1];
	drawFrame(a, b, row, col);
}


void drawPlaying()
{
	SetConsoleTextAttribute(hConsole, 13);
	drawFrame(0, 0, 48, 24, '=', '|');//    draw map frame;  
	drawFrame(49, 0, 79, 4, '-', '|');//        draw output frame  
	drawFrame(49, 4, 79, 9, '-', '|');//        draw score frame  
	drawFrame(49, 9, 79, 20, '-', '|');//   draw operate frame  
	drawFrame(49, 20, 79, 24, '-', '|');//  draw other message frame 
	drawFrame(86, 0, 99, 2, '-', '|');
	drawFrame(86, 3, 99, 5, '-', '|');
	SetConsoleTextAttribute(hConsole, 12);
	SetPos(81, 1);
	cout << "血量";
	SetConsoleTextAttribute(hConsole, 9);
	SetPos(81, 4);
	cout << "蓝量";
	SetConsoleTextAttribute(hConsole, 6);
	SetPos(52, 6);
	cout << "得分：";
	SetPos(52, 7);
	cout << "称号：";
	SetPos(52, 10);
	cout << "操作方式：";
	SetPos(52, 12);
	cout << "  a,s,d,w 控制战机移动。";
	SetPos(52, 14);
	cout << "  k 发射子弹 f 发射激光。";
	SetPos(52, 16);
	cout << "  p 暂停游戏。";
	SetPos(52, 18);
	cout << "  esc 退出游戏并保存。";
}

//在[a, b)之间产生一个随机整数  
int random(int a, int b)
{
	int c = (rand() % (a - b)) + a;
	return c;
}

//在两个坐标包括的矩形框内随机产生一个坐标  
COORD random(COORD a, COORD b)
{
	int x = random(a.X, b.X);
	int y = random(a.Y, b.Y);
	COORD c = { x, y };
	return c;
}

bool  judgeCoordInFrame(Frame frame, COORD spot)//判断敌人中弹
{
	if (spot.X >= frame.position[0].X)
		if (spot.X <= frame.position[0].X + 6)
			if (spot.Y >= frame.position[0].Y)
				if (spot.Y <= frame.position[1].Y)
					return true;
	return false;
}
bool  judgeCoordInBossBullet(COORD frame, COORD spot)//被boss打
{
	if (spot.Y == frame.Y)
		if (spot.X == frame.X)
			return true;
	if (spot.Y + 1 == frame.Y)
		if (spot.X + 2 >= frame.X && spot.X - 2 <= frame.X)
			return true;   //右边碰撞
	if (spot.Y + 2 == frame.Y)
		if (spot.X + 4 >= frame.X && spot.X - 4 <= frame.X)
			return true;
	return false;
}

bool  judgeCoordInBoss(COORD frame, COORD spot)//打boss
{
	if (spot.Y == frame.Y)
		if (spot.X == frame.X)
			return true;
	if (spot.Y - 1 == frame.Y)
		if (spot.X + 2 >= frame.X && spot.X - 2 <= frame.X)
			return true;
	if (spot.Y - 2 == frame.Y)
		if (spot.X + 4 >= frame.X && spot.X - 4 <= frame.X)
			return true;
	if (spot.Y - 3 == frame.Y)
		if (spot.X + 8 >= frame.X && spot.X - 8 <= frame.X)
			return true;
	return false;
}
bool judgeCoordInFrameofplane(Frame frame, COORD spot)//判断自己飞机被撞
{
	if (spot.Y == frame.position[1].Y)
		if (spot.X == frame.position[1].X)
			return true;
	if (spot.Y == frame.position[0].Y)
		if (spot.X >= frame.position[1].X && spot.X <= frame.position[1].X + 6)
			return true;
	if (spot.Y + 1 == frame.position[0].Y)
		if (spot.X + 4 >= frame.position[0].X && spot.X + 4 <= frame.position[0].X + 6)
			return true;   //右边碰撞
	if (spot.Y + 1 == frame.position[0].Y)
		if (spot.X - 2 <= frame.position[0].X + 6 && spot.X - 2 >= frame.position[0].X)
			return true;    //左边碰撞
	if (spot.Y + 2 == frame.position[0].Y)
		if (spot.X + 5 <= frame.position[0].X + 6 && spot.X + 5 >= frame.position[0].X)
			return true;
	if (spot.Y + 2 == frame.position[0].Y)
		if (spot.X - 3 <= frame.position[0].X + 6 && spot.X - 3 >= frame.position[0].X)
			return true;
	return false;
}



int drawMenu2()
{
	system("color 7F");
	cyan_choose();
	SetPos(0, 0);
	cout << "w 和 s 选择， k 确定";

	blue_border();
	SetPos(5, 5);
	cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";//上边框
	SetPos(5, 25);
	cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";//下边框
	for (int i = 5, j = 6; j < 25; j++) //左边框
	{
		SetPos(i, j);
		cout << "*";
	}
	for (int i = 85, j = 6; j < 25; j++) //右边框
	{
		SetPos(i, j);
		cout << "*";
	}
	on_Select();
	SetPos(36, 8);
	cout << "S P A C E  W A R";
	SetPos(15, 11);
	on_Select();
	cout << "1. 简单的敌人";
	SetPos(15, 13);
	cyan_choose();
	cout << "2. 冷酷的敌人";
	SetPos(47, 11);
	on_Select();
	cout << "移动速度较慢";
	int j = 0;
	while (1)
	{
		if (_kbhit())
		{
			char x = _getch();
			switch (x)
			{
			case 'w':
			{
				j = 0;
				SetPos(15, 11);
				on_Select();
				cout << "1. 简单的敌人";
				SetPos(15, 13);
				cyan_choose();
				cout << "2. 冷酷的敌人";
				SetPos(47, 11);
				on_Select();
				cout << "移动速度较慢";
				break;
			}
			case 's':
			{
				j = 1;
				SetPos(15, 11);
				cyan_choose();
				cout << "1. 简单的敌人";
				SetPos(15, 13);
				cyan_choose();
				on_Select();
				cout << "2. 冷酷的敌人";
				SetPos(47, 11);
				on_Select();
				cout << "移动速度较快";
				break;
			}
			case 'k':
			{
				if (j == 0)
				{
					white_back();
					return 1;
				}
				else if (j == 1)
				{
					white_back();
					return 2;
				}
			}
			}
		}
	}
	white_back();
}

int drawMenu()
{
	system("color 7F");//设置控制台界面背景颜色和前景颜色
	system("mode con cols=100 lines=40");//初始化缓冲区大小
	SetConsoleTitle("SPACE WAR");//设置控制台窗口标题
	cyan_choose();
	cout << "w,a,s,d 控制光标选择；空格 确定" << endl;

	blue_border();
	SetPos(5, 5);
	cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";//上边框
	SetPos(5, 25);
	cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";//下边框
	for (int i = 5, j = 6; j < 25; j++) //左边框
	{
		SetPos(i, j);
		cout << "*";
	}
	for (int i = 85, j = 6; j < 25; j++) //右边框
	{
		SetPos(i, j);
		cout << "*";
	}

	cyan_choose();
	SetPos(65, 10);
	cout << "2. 继续 ";
	SetPos(25, 20);
	cout << "3. 积分榜 ";
	SetPos(65, 20);
	cout << "4. 退出";
	on_Select();
	SetPos(25, 10);
	cout << "1. 开始 ";


	int x = 25, y = 10;
	char sel;
	sel = _getch();
	while (sel != ' ')
	{
		star();
		switch (sel)
		{
		case 'w':
			y = y - 10;
			break;
		case 's':
			y = y + 10;
			break;
		case 'a':
			x = x - 40;
			break;
		case 'd':
			x = x + 40;
			break;
		}
		//防止超出范围
		if (x >= 65)
		{
			x = 65;
		}
		if (y >= 20)
		{
			y = 20;
		}
		if (x <= 25)
		{
			x = 25;
		}
		if (y <= 10)
		{
			y = 10;
		}
		SetPos(x, y);
		onChoose(x, y);
		sel = _getch();
	}

	SetPos(0, 30);
	white_back();//还原默认字体
	if (x == 25 && y == 10)
		return 1;
	if (x == 65 && y == 10)
		return 2;
	if (x == 25 && y == 20)
		return 3;
	if (x == 65 && y == 20)
		return 4;
}

void drawenemylook(Frame frame)
{
	SetConsoleTextAttribute(hConsole, 8);
	SetPos(frame.position[0]);
	cout << "Ψ█Ψ";
	SetPos(frame.position[1]);
	cout << "▼";
}

void recordgoal(int s)
{
	ofstream file("Date.txt", ios::app);//在结束游戏时记录所得分数，分数直接添加到文件末尾
	if (!file)
	{
		cerr << "File cannot be opened" << endl;
		exit(EXIT_FAILURE);
	}
	system("cls");
	cout << "Please enter your name:";
	string str;
	cin >> str;
	file << endl << str << setw(5) << s;
	file.close();
}

void Game::Integraltable()
{
	system("cls");
	ifstream infile("Date.txt", ios::in);//打开文件读取数据
	if (!infile)
	{
		cerr << "File cannot be opened" << endl;
		exit(EXIT_FAILURE);
	}
	/*存储数据*/
	vector<int>v;
	string str[100];
	for (int i = 0; !infile.eof(); i++)
	{
		int t1;
       infile >> str[i] >> t1;
		v.push_back(t1);
	}
	cout << endl;

	for (int i = 0; i <= v.size() - 1; i++)//选择排序
	{
		int max = v[i];
		int max_index = i;
		for (int j = i; j <= v.size() - 1; j++)
		{
			if (v[j] > max)
			{
				max = v[j];
				max_index = j;
			}
		}

		if (max_index != i)
		{
			swap(v[i], v[max_index]);
			swap(str[i], str[max_index]);
		}
	}
	cout <<
		"＃〓§〓〓〓〓〓〓〓〓〓〓§〓〓〓〓〓〓〓〓〓〓〓§〓〓〓" << endl;
	cout << "    ↓　　　　　          ↓　　　　　     　     ↓　　　" << endl;
	cout << "  ☆★☆ 　             ☆★☆　　　          　☆★☆ 　　" << endl
		<< "  ☆积☆　              ☆分☆　　              ☆表☆　" << endl
		<< "  ☆★☆  　            ☆★☆　　　          　☆★☆ 　" << endl
		<< "  ︻━╋████◤                            ◥██▇▆▅︻" << endl;
	
	cout << endl;
	cout << "rank" << setw(18) << "name" << setw(22) << "score" << endl;
	for (int i = 0; i < v.size(); i++)
		if (v[i] >= 0)
			cout << i + 1 << "." << setw(20) << str[i] << setw(20) << v[i] << endl;

	int ss;
	if (_kbhit)
		ss = _getch();
	if (27 == ss)
	{
		system("cls");
		GameP2();
	}
}

void Game::Show_HP(int n)
{
	SetPos(87, 1);
	for (int i = 0; i < 6; i++)
		cout << "  ";
	SetPos(87, 1);
	for (int i = 0; i < n; i++)
	{
		SetConsoleTextAttribute(hConsole, 12);
		cout << "■";
	}
}

void Game::Show_MP(int n)
{
	SetPos(87, 4);
	for (int i = 0; i < 6; i++)
		cout << "  ";
	SetPos(87, 4);
	for (int i = 0; i < n; i++)
	{
		SetConsoleTextAttribute(hConsole, 9);
		cout << "■";
	}
}

void Game::initPlane()
{
	COORD centren = { lastposofPlane.X,lastposofPlane.Y };
	position[0].X = centren.X;
	position[0].Y = centren.Y;
	position[1].X = centren.X - 2;
	position[1].Y = centren.Y + 1;
	position[2].X = centren.X - 4;
	position[2].Y = centren.Y + 2;
}

void Game::drawPlane()
{
	SetConsoleTextAttribute(hConsole, 3);
	SetPos(position[0]);
	cout << "▲";
	SetPos(position[1]);
	cout << "███";
	SetPos(position[2]);
	cout << "◢█$$█◣";
}

void Game::drawPlaneToNull()
{
	SetPos(position[0]);
	cout << "  ";
	SetPos(position[1]);
	cout << "      ";
	SetPos(position[2]);
	cout << "          ";
}

void Game::initBullet()
{
	for (int i = 0; i < 10; i++)
		bullet[i].Y = 30;
}

void Game::drawBullet()
{
	for (int i = 0; i < 10; i++)
	{
		if (bullet[i].Y != 30)
		{
			SetPos(bullet[i]);
			SetConsoleTextAttribute(hConsole, 12);
			cout << "^";
		}
	}
}

void Game::drawBulletToNull()
{
	for (int i = 0; i < 10; i++)
		if (bullet[i].Y != 30)
		{
			COORD pos = { bullet[i].X, bullet[i].Y + 1 };
			SetPos(pos);
			cout << " ";
		}
}

void Game::initEnemy()
{
	COORD a = { 1, 5 };
	COORD b = { 38, 15 };
	for (int i = 0; i < 2; i++)
	{
		enemy[i].position[0] = random(a, b);//在a。X到b.X上随机出现敌人
		enemy[i].position[1].X = enemy[i].position[0].X + 2;
		enemy[i].position[1].Y = enemy[i].position[0].Y + 1;
	}
}

void Game::drawEnemy()  //输出敌人的边框
{
	for (int i = 0; i < 2; i++)
	{
		drawenemylook(enemy[i]);
	}
}

void Game::drawEnemyToNull()  //输出敌人图案间的空格
{
	for (int i = 0; i < 2; i++)
	{
		SetPos(enemy[i].position[0]);
		cout << "      ";
		SetPos(enemy[i].position[1]);
		cout << "  ";
	}
}


void Game::Pause()
{
	SetPos(61, 2);
	cout << "               ";
	SetPos(61, 2);
	cout << "暂停中...";
	char c = _getch();
	while (c != 'p')
		c = _getch();
	SetPos(61, 2);
	cout << "         ";
}

void Game::planeMove(char x)	
{
	if (x == 'a')
		if (position[2].X != 1)
			for (int i = 0; i <= 2; i++)
				position[i].X -= 2;

	if (x == 's')
		if (position[2].Y != 23)
			for (int i = 0; i <= 2; i++)
				position[i].Y += 1;

	if (x == 'd')
		if (position[2].X != 37)
			for (int i = 0; i <= 2; i++)
				position[i].X += 2;

	if (x == 'w')
		if (position[0].Y != 3)
			for (int i = 0; i <= 2; i++)
				position[i].Y -= 1;
}

void Game::bulletMove()
{
	for (int i = 0; i < 10; i++)
	{
		if (bullet[i].Y != 30)         //30纯粹是为了跳出循环，清空数据	
		{
			bullet[i].Y -= 1;
			if (bullet[i].Y == 1)
			{
				COORD pos = { bullet[i].X, bullet[i].Y + 1 };
				drawThisBulletToNull(pos);
				bullet[i].Y = 30;
			}

		}
	}
}

void Game::enemyMove()
{
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 2; j++)
			enemy[i].position[j].Y++;
             //srand(time(0));
		if (24 <= enemy[i].position[1].Y)
		{
			if (boss[0].Y > 10)
			{
				COORD a = { 1, 1 };
				COORD b = { 39, 3 };//给boss腾出位置
				//
				enemy[i].position[0] = random(a, b);
				enemy[i].position[1].X = enemy[i].position[0].X + 2;
				enemy[i].position[1].Y = enemy[i].position[0].Y + 1;
			}
			else
			{
				COORD a = { 1, 7 };
				COORD b = { 39, 9 };
				//srand(time(0));
				enemy[i].position[0] = random(a, b);
				enemy[i].position[1].X = enemy[i].position[0].X + 2;
				enemy[i].position[1].Y = enemy[i].position[0].Y + 1;
			}

		}
		

			
	}
}



void Game::judgePlane()
{
	for (int i = 0; i < 2; i++)
		if (judgeCoordInFrameofplane(enemy[i], position[0]))//敌机撞了之后扣血
		{
			Sleep(100);
			--HP;
			judgeHP();
			continue;
		}
	for (int i = 0; i < 1000; i++)
		if (judgeCoordInBossBullet(BossBullet[i], position[0]))//被boss子弹打中扣血
		{
			drawThisBulletToNull(BossBullet[i]);
			BossBullet[i].Y = 60;
			Sleep(100);
			--HP;
			judgeHP();
			continue;
		}
}

void Game::judgeHP()
{

	if (HP == 0)
	{
		SetPos(87, 1);
		for (int i = 0; i < 6; i++)
			cout << "  ";
		SetPos(62, 1);
		cout << "坠毁";
		Sleep(1000);
		GameOver();
	}
}
void Game::drawThisBulletToNull(COORD c)
{
	SetPos(c);
	cout << " ";

}
void Game::drawThisEnemyToNull(Frame f)
{
	for (int i = 0; i < 2; i++)
	{
		SetPos(enemy[i].position[0]);
		cout << "      ";
		SetPos(enemy[i].position[1]);
		cout << "  ";
	}
}

void Game::judgeEnemy()
{
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 10; j++)
			if (judgeCoordInFrame(enemy[i], bullet[j]) || judgeCoordInFrameofplane(enemy[i], position[0]))
			{
				score += 5;
				drawThisEnemyToNull(enemy[i]);

				for (int j = 0; j < 2; j++)
				{
					if (boss[0].Y > 10)
					{
						COORD a = { 1, 1 };
						COORD b = { 39, 3 };//给boss腾出位置
						enemy[i].position[0] = random(a, b);
						enemy[i].position[1].X = enemy[i].position[0].X + 2;
						enemy[i].position[1].Y = enemy[i].position[0].Y + 1;
					}
					else
					{
						COORD a = { 1, 7 };
						COORD b = { 39, 9 };

						enemy[i].position[0] = random(a, b);
						enemy[i].position[1].X = enemy[i].position[0].X + 2;
						enemy[i].position[1].Y = enemy[i].position[0].Y + 1;
					}
					if (i == j)
						continue;
					else
					{
						if (enemy[j].position[0].X > enemy[i].position[0].X - 6 && enemy[j].position[0].Y < enemy[i].position[0].Y + 2 && enemy[j].position[0].X < enemy[i].position[0].X + 6)//尽量让敌机分开来，不会粘在一起；
						{
							j--;
							continue;
						}
						else if (enemy[j].position[0].Y == enemy[i].position[1].Y)
							if (enemy[j].position[0].X <= enemy[i].position[1].X)
								if (enemy[j].position[0].X + 6 >= enemy[i].position[1].X)
								{
									j--;
									continue;
								}
								else
									break;

					}
				}


				drawThisBulletToNull(bullet[j]);
				bullet[j].Y = 30;
			}
}

void Game::judgeBoss()
{
	for (int i = 0; i < 10; i++)
	{
		if (judgeCoordInBoss(bullet[i], boss[3]))
		{
			drawThisBulletToNull(bullet[i]);//+
			bullet[i].Y = 30;//+
			--BossHP;
			int a = rand() % 16;
			SetConsoleTextAttribute(hConsole, a);
			drawBoss();
			if (BossHP <= 0)
			{
				score += 20;
				drawBossToNull();
			}
		}
	}
}
void Game::initBoss(int x, int y)
{
	COORD position = { x,y };
	boss[0].X = position.X;
	boss[0].Y = position.Y;
	boss[1].X = position.X + 4;
	boss[1].Y = position.Y + 1;
	boss[2].X = position.X + 6;
	boss[2].Y = position.Y + 2;
	boss[3].X = position.X + 8;
	boss[3].Y = position.Y + 3;
}
void Game::drawBoss()
{
	if (BossHP == 100)
		SetConsoleTextAttribute(hConsole, 11);
	SetPos(boss[0]);
	cout << "ΨΨΨ███ΨΨΨ";
	SetPos(boss[1]);
	cout << "Ψ$$█$$Ψ";
	SetPos(boss[2]);
	cout << "Ψ█Ψ";
	SetPos(boss[3]);
	cout << "▼";

}

void Game::drawBossToNull()
{
	SetPos(boss[0]);
	cout << "                  ";
	SetPos(boss[1]);
	cout << "          ";
	SetPos(boss[2]);
	cout << "      ";
	SetPos(boss[3]);
	cout << "  ";
	initBoss(100, 100);
}



void Game::initBossBullet()
{
	COORD start = { boss[3].X ,boss[3].Y };
	BossBullet[j].X = start.X + 1;
	BossBullet[j].Y = start.Y + 1;
	j++;
}

void Game::drawBossBullet()
{
	for (int i1 = k; i1 < j; i1++)
	{
		SetPos(BossBullet[i1]);
		cout << "|";
	}
}

void drawThisBossBulletToNull(COORD c)
{
	SetPos(c);
	cout << " ";
	c = { 100,100 };
}
void Game::drawBossBulletToNull()
{
	for (int i2 = k; i2 < j; i2++)
	{
		COORD pos = { BossBullet[i2].X, BossBullet[i2].Y };
		SetPos(pos);
		cout << " ";
		BossBullet[i2] = { 100,100 };
	}
}


void Game::BossBulletMove()
{
	drawBossBullet();
	int flag = 0;
	while (1)
	{
		if (flag % 3 == 0)
		{
			initBoss(p, s);
			drawBoss();
			Sleep(400);
			drawBossToNull();

			if (p >= 1 && p < 28)
				p += 1;
			else if (p == 28)
			{
				p = 1;
			}
		}
		if (flag % 10 == 0)
			initBossBullet();
		if (flag % 2 == 0)
		{
			for (int i = k; i < j; i++)
			{
				COORD pos = { BossBullet[i].X, BossBullet[i].Y };
				drawThisBossBulletToNull(pos);
				if (BossBullet[i].Y <= 20)
				{
					BossBullet[i].Y += 1;
					drawBossBullet();

				}
				else
				{
					k++;
				}
			}
		}
		flag++;
	}
}


void Game::Shoot()
{
	for (int i = 0; i < 10; i++)
		if (bullet[i].Y == 30)
		{
			bullet[i].X = position[0].X;
			bullet[i].Y = position[0].Y - 1;
			break;
		}
}

void Game::BossShoot()
{
	BossBulletMove();
}

Game::Game()
{

	initPlane();
	score = lastscore;
	rank = lastrank;
	rankf = lastrankf;
	flag_rank = lastflag_rank;
	if (if_continue == 0)
	{
		p = 1;
		s = 1;
		initBullet();
		initEnemy();
		rank = 25;
		score = 0;
		rankf = 0;
		flag_rank = 0;

		HP = 6;
		MP = 6;
		BossHP = 20;
	}



}


void Game::printScore()
{
	if (score == 120 && flag_rank == 0)
	{
		rank -= 3;
		flag_rank = 1;
	}

	else if (score == 360 && flag_rank == 1)
	{
		rank -= 5;
		flag_rank = 2;
	}
	else if (score == 480 && flag_rank == 2)
	{
		rank -= 5;
		flag_rank = 3;
	}
	int x = rank / 5;
	SetPos(60, 6);
	cout << score;

	if (rank != rankf)
	{
		SetPos(60, 7);
		if (x == 5)
			title = "初级飞行员";
		else if (x == 4)
			title = "中级飞行员";
		else if (x == 3)
			title = "高级飞行员";
		else if (x == 2)
			title = "王牌飞行员";
		cout << title;
	}
}

void Game::Playing()
{
	drawBossBullet();
	drawEnemy();
	drawPlane();
	int flag = 0;
	/*放背景音乐*/
	PlaySound("13到36.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
	int flag_bullet = 0;
	int flag_EnemyBullet = 30;
	int flag_enemy = 0;

	while (1)
	{
		Sleep(8);
		if (_kbhit())
		{
			char x = _getch();
			if ('a' == x || 's' == x || 'd' == x || 'w' == x)
			{
				drawPlaneToNull();
				planeMove(x);
				drawPlane();
				judgePlane();
			}
			else if ('p' == x)
				Pause();
			else if ('k' == x)
				Shoot();
			else if ('f' == x)
			{
				if (MP > 0)
				{

					DrawRectangle(position[0].X, position[0].Y, BLUE | INTENSITY);

					for (int i = 0; i < 2; i++)
						if (enemy[i].position[0].X - 1 <= position[0].X && enemy[i].position[0].X + 5 >= position[0].X)
						{
							score += 5;
							drawThisEnemyToNull(enemy[i]);
							for (int j = 0; j < 2; j++)
							{
								if (boss[0].Y > 10)
								{
									COORD a = { 1, 1 };
									COORD b = { 39, 3 };//给boss腾出位置
									enemy[i].position[0] = random(a, b);
									enemy[i].position[1].X = enemy[i].position[0].X + 2;
									enemy[i].position[1].Y = enemy[i].position[0].Y + 1;
								}
								else
								{
									COORD a = { 1, 7 };
									COORD b = { 39, 9 };

									enemy[i].position[0] = random(a, b);
									enemy[i].position[1].X = enemy[i].position[0].X + 2;
									enemy[i].position[1].Y = enemy[i].position[0].Y + 1;
								}
								if (i == j)
									continue;
								else
								{
									if (enemy[j].position[0].X > enemy[i].position[0].X - 6 && enemy[j].position[0].Y < enemy[i].position[0].Y + 2 && enemy[j].position[0].X < enemy[i].position[0].X + 6)//让敌机分开来，不会粘在一起；
									{
										j--;
										continue;
									}
									else if (enemy[j].position[0].Y == enemy[i].position[1].Y)
										if (enemy[j].position[0].X <= enemy[i].position[1].X)
											if (enemy[j].position[0].X + 6 >= enemy[i].position[1].X)
											{
												j--;
												continue;
											}
											else
												break;

								}
							}
						}
				}
				--MP;
			}
			else if (27 == x)  //按esc键返回主菜单
			{
				if_continue = 1;
				PlaySound(NULL, NULL, SND_FILENAME);
				system("cls");

				int b = 0;
				int a = drawMenu();

				if (a == 1)
				{
					if_continue = 0;
					lastposofPlane = { 35,21 };
					Game game;
					p = 1; s = 1;
					drawBossBulletToNull();
					system("cls");
					b = drawMenu2();
					system("cls");
					if (b == 2)
						game.rank = 20;
					drawPlaying();
					game.Playing();
				}
				if (a == 3)
					Integraltable();
				if (a == 4)
					exit(0);
				system("cls");
				drawPlaying();
				Playing();
			}

		}
		/* 处理子弹 */
		if (0 == flag_bullet)
		{
			bulletMove();
			drawBulletToNull();
			drawBullet();
			judgeEnemy();
			judgeBoss();
		}
		flag_bullet++;
		if (5 == flag_bullet)
			flag_bullet = 0;

		/*敌方飞机子弹*/

		flag_EnemyBullet--;
		if (5 == flag_bullet)
			flag_bullet = 0;
		/* 处理敌人 */
		if (0 == flag_enemy)
		{
			drawEnemyToNull();
			enemyMove();
			drawEnemy();
			judgePlane();
			Show_HP(HP);
			Show_MP(MP);

		}
		flag_enemy++;
		if (flag_enemy >= rank)
			flag_enemy = 0;
		//boss

		if (flag % 40 == 0 && BossHP > 0)//3
		{
			drawBossToNull();
			initBoss(p, s);
			drawBoss();
			srand(time(0));
			if (p >= 1 && p < 28)
			{
				p = p + rand() % 4;

			}
			else if (p >= 28)
			{
				p = 1;
			}


		}

		if (flag % 250 == 0 && BossHP > 0)//控制发子弹的频率
		{
			initBossBullet();
		}
		if (flag % 30 == 0 && BossHP > 0)
		{
			for (int i = k; i < j; i++)
			{
				COORD pos = { BossBullet[i].X, BossBullet[i].Y };
				drawThisBossBulletToNull(pos);
				if (BossBullet[i].Y <= 20)
				{
					BossBullet[i].Y += 1;
					drawBossBullet();

				}
				else
				{

					k++;
					BossBullet[i] = { 100,100 };
				}

			}
		}
		if (BossHP <= 0)
			drawBossBulletToNull();
		if (flag % 1000 == 0 && BossHP == 0)
		{
			BossHP = 20;
			p = 1, s = 1;
		}
		flag++;
		/* 输出得分 */
		SetConsoleTextAttribute(hConsole, 9);
		printScore();
	}
}

void Game::GameOver()
{
	PlaySound(NULL, NULL, SND_FILENAME);//暂停背景音乐
	recordgoal(score);
	system("cls");
	COORD p1 = { 28,9 };
	COORD p2 = { 53,15 };
	drawFrame(p1, p2, '=', '|');
	SetPos(36, 12);
	string str = "Game Over!";
	for (int i = 0; i < str.size(); i++)
	{
		Sleep(80);
		cout << str[i];
	}
	Sleep(1000);
	system("cls");
	drawFrame(p1, p2, '=', '|');
	SetPos(31, 11);
	cout << "击落敌机：" << score / 5 << " 架";
	SetPos(31, 12);
	cout << "得　　分：" << score;
	SetPos(31, 13);
	cout << "获得称号：" << title;
	SetPos(30, 16);
	Sleep(1000);
	cout << "继续？ 是（y）| 否（n）制作：义才+涌盛";
as:
	char x = _getch();
	if (x == 'n')
		exit(0);
	else if (x == 'y')
	{
		system("cls");
		Game game;
		system("cls");
		game.GameP2();
	}
	else goto as;
}
/*5.8日加的函数*/
void Game::GameP2()
{
	int b = 0;
	int a;

	do
	{
		a = drawMenu();
		if (a == 1)
		{
			drawBossBulletToNull();
			Game game;
			system("cls");
			b = drawMenu2();
			if (b == 2)
				rank = 20;
			system("cls");
			drawPlaying();
			game.Playing();
		}
		if (a == 2)
		{
			if (if_continue == 1)
			{
				system("cls");
				drawPlaying();
				Playing();
			}
			else
			{
				SetPos(0, 90);
				cout << "Sorry, you didn't begin the gane." << endl;
				Sleep(1500);
			}
		}
		if (a == 3)
			Integraltable();
		if (a == 4)
			exit(0);
	} while (a != 4);
}
void playvideo()
{
	CharacterVideo cv;
	cv.recursor(0, 0);//设置光标位置
	char fileName[50] = { "飞机的txt\\tu (1).txt" };
	string str;
	clock_t starttime, finishtime;
	ifstream File(fileName);
	int i = 0, delayTime = 33;
	cin.get();
	starttime = clock();
	PlaySound("开头.wav", NULL, SND_FILENAME | SND_ASYNC);
	Sleep(1000);
	while (i < 520)
	{

		if (i % 30 == 0)
			delayTime = 40;
		else
			delayTime = 30;
		cv.renewfileName(fileName, i);
		finishtime = clock();
		if ((finishtime - starttime) >= delayTime)//让音效和画面更契合

		{
			i++;
			File.open(fileName);
			while (File.good())//只要能读到东西 就return true 如果遇到错误和结束输入，return false
			{
				getline(File, str);
				cout << str;
			}
			File.close();

			starttime += delayTime;
			cv.recursor(0, 0);//每输出一张图片都要让光标归位；比system("cls")好用；
		}
	}

	cin.get();
	PlaySound(NULL, NULL, SND_FILENAME);//停止播放音乐
}
/*================== the main function ==================*/
int main()
{
	playvideo();//播放视频
	//游戏准备  
	srand((int)time(0));    //随机种子  
	HideCursor();   //隐藏光标  

	Game game;
	game.GameP2();

}


